package com.mission.schedule.CommonDialog;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.StyleRes;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.mission.schedule.R;
import com.mission.schedule.activity.NoteTypepxActivity;
import com.mission.schedule.activity.TagSerachActivity;
import com.mission.schedule.applcation.App;
import com.mission.schedule.bean.TagCommandBean;
import com.mission.schedule.constants.ShareFile;
import com.mission.schedule.service.DownCalenderNoteService;
import com.mission.schedule.service.DownCalenderService;
import com.mission.schedule.utils.NetUtil;
import com.mission.schedule.utils.SharedPrefUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * 日程顶部对话框
 * Created by liwei on 2017/4/18.
 */

public class CalenderTopCenterDialog extends Dialog {


    CallBack callBack;

    SharedPrefUtil sharedPrefUtil;
    Context context;
    WindowManager windowManager;
    View view;
    LinearLayout sjb_ll, top_ll_back, search_button;
    TextView title_tv;
    List<TagCommandBean> list;
    GridView gridView,gridView1, gridView2;
    String gs[] = {"以前", "重要", "待办","搜索"};
    int gst[] = {-1, -2, -3, 0};

    public CalenderTopCenterDialog(@NonNull final Context context, @StyleRes int themeResId, WindowManager windowManager) {
        super(context, themeResId);
        this.context = context;
        this.windowManager = windowManager;

        setCanceledOnTouchOutside(true);

        view = LayoutInflater.from(context).inflate(R.layout.calender_top_center, null);
        setContentView(view);

        sjb_ll = view.findViewById(R.id.sjb_ll);
        title_tv = view.findViewById(R.id.title);
        top_ll_back = view.findViewById(R.id.top_ll_back);
        search_button = view.findViewById(R.id.search_button);
        gridView = view.findViewById(R.id.gridView);
        gridView1 = view.findViewById(R.id.gridView1);
        gridView2 = view.findViewById(R.id.gridView2);

        list = App.getDBcApplication().QueryTagData(10);

        TagCommandBean map = new TagCommandBean();
        map.setCtgText("分类管理");
        map.setCtgId("00");
        list.add(map);
        TagCommandBean map1 = new TagCommandBean();
        map1.setCtgText("到期日");
        map1.setCtgId("01");
        list.add(0,map1);

        gridView.setAdapter(new Adapter());
        gridView1.setAdapter(new Adapter1());

        sharedPrefUtil = new SharedPrefUtil(context, ShareFile.USERFILE);

        String TYPEINDEX = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.TYPEINDEX, "3");
        String title = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.TYPETITLE, "我的日程");
        int newopeartType = Integer.valueOf(TYPEINDEX);
        if (newopeartType < 0) {
            top_ll_back.setVisibility(View.VISIBLE);
        } else {
            top_ll_back.setVisibility(View.GONE);
        }
        title_tv.setText(title);

        if (!sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.SJB, "").isEmpty()) {
            sjb_ll.setVisibility(View.VISIBLE);
            String[] sjbs = sharedPrefUtil.getString(context, ShareFile.USERFILE, ShareFile.SJB, "").split(",,");
            String[] names = sjbs[0].replace("[", "").replace("]", "").split(",");
            String[] uids = sjbs[1].replace("[", "").replace("]", "").split(",");
            String[] types = sjbs[2].replace("[", "").replace("]", "").split(",");
            gridView2.setAdapter(new Adapter2(names, uids, types));
        }

        if (!title_tv.getText().toString().equals("我的日程")) {
            top_ll_back.setVisibility(View.VISIBLE);
        } else {
            top_ll_back.setVisibility(View.GONE);
        }
        title_tv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        search_button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                context.startActivity(new Intent(context, TagSerachActivity.class));
            }
        });
        top_ll_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
                sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.TYPEUID, "");
                callBack.setType(0, "我的日程");
            }
        });
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        window.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
        params.width = windowManager.getDefaultDisplay().getWidth();
        params.height = ViewGroup.LayoutParams.WRAP_CONTENT;
        window.setAttributes(params);
    }

    public interface CallBack {
        void setType(int index, String title);
    }

    public CallBack getCallBack() {
        return callBack;
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }


    class Adapter extends BaseAdapter {

        @Override
        public int getCount() {
            return gs.length;
        }

        @Override
        public Object getItem(int position) {
            return gs[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            Holder holder;
            if (convertView == null) {
                holder = new Holder();
                convertView = LayoutInflater.from(context).inflate(R.layout.top_textview, parent, false);
                holder.tv = convertView.findViewById(R.id.top_tv);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }

            holder.tv.setText(gs[position]);

            holder.tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.TYPEUID, "");

                    if (gst[position] == 0) {
                        context.startActivity(new Intent(context, TagSerachActivity.class));
                    } else {
                        callBack.setType(gst[position], gs[position]);
                    }

                }
            });
            return convertView;
        }

        class Holder {
            TextView tv;
        }
    }

    class Adapter1 extends BaseAdapter {

        @Override
        public int getCount() {
            return list.size();
        }

        @Override
        public Object getItem(int position) {
            return list.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            Holder holder;
            if (convertView == null) {
                holder = new Holder();
                convertView = LayoutInflater.from(context).inflate(R.layout.top_textview, parent, false);
                holder.tv = convertView.findViewById(R.id.top_tv);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }

            holder.tv.setText(list.get(position).ctgText);
            if (list.get(position).ctgId.equals("00")) {
                holder.tv.setBackground(context.getResources().getDrawable(R.drawable.calender_new_detail_item_bg_bo));
            }

            holder.tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (list.get(position).ctgId.equals("01")) {
                        dismiss();
                        sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.TYPEUID, "");
                        callBack.setType(-4, list.get(position).ctgText);
                    } else if (list.get(position).ctgId.equals("00")) {
                        dismiss();
                        Intent intent = new Intent(context, NoteTypepxActivity.class);
                        intent.putExtra("type", 0);
                        context.startActivity(intent);
                    } else {
                        dismiss();
                        sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.TYPEUID, "");
                        callBack.setType(- 5 - Integer.valueOf(list.get(position).ctgDesc), list.get(position).ctgText);
                    }
                }
            });
            return convertView;
        }

        class Holder {
            TextView tv;
        }
    }

    class Adapter2 extends BaseAdapter {

        String[] names;
        String[] uids;
        String[] types;

        public Adapter2(String[] names, String[] uids, String[] types) {
            this.names = names;
            this.uids = uids;
            this.types = types;
        }

        @Override
        public int getCount() {
            return names.length;
        }

        @Override
        public Object getItem(int position) {
            return names[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            Holder holder;
            if (convertView == null) {
                holder = new Holder();
                convertView = LayoutInflater.from(context).inflate(R.layout.top_textview, parent, false);
                holder.tv = convertView.findViewById(R.id.top_tv);
                convertView.setTag(holder);
            } else {
                holder = (Holder) convertView.getTag();
            }

            holder.tv.setText(names[position]);

            if (types[position].equals("1")) {
                holder.tv.setTextColor(context.getResources().getColor(R.color.Saturdaybackcolor));
            }

            holder.tv.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    dismiss();
                    sharedPrefUtil.putString(context, ShareFile.USERFILE, ShareFile.TYPEUID, uids[position]);
                    callBack.setType(-91, names[position]);
                    if (NetUtil.getConnectState(context) != NetUtil.NetWorkState.NONE) {
                        Intent downRepeat = new Intent(context, DownCalenderService.class);
                        downRepeat.setAction("DownCalender");
                        downRepeat.putExtra("isOnRefresh", false);
                        downRepeat.setPackage(context.getPackageName());
                        context.startService(downRepeat);
                    }
                }
            });
            return convertView;
        }

        class Holder {
            TextView tv;
        }
    }
}
